home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / FTELL.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  1KB  |  55 lines

  1. /* This is file FTELL.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. #if defined(LIBC_SCCS) && !defined(lint)
  8. static char sccsid[] = "@(#)ftell.c    5.2 (Berkeley) 3/9/86";
  9. #endif LIBC_SCCS and not lint
  10.  
  11. /*
  12.  * Return file offset.
  13.  * Coordinates with buffering.
  14.  */
  15.  
  16. #include    <stdio.h>
  17. long    lseek();
  18.  
  19.  
  20. long ftell(iop)
  21. register FILE *iop;
  22. {
  23.     register long tres;
  24.     register adjust;
  25.     int idx;
  26.  
  27.     if (iop->_cnt < 0)
  28.         iop->_cnt = 0;
  29.     if (iop->_flag&_IOREAD)
  30.     {
  31.         adjust = - iop->_cnt;
  32.         if (iop->_flag & _IOTEXT)            /* if a text file */
  33.           for (idx=-adjust-1; idx>=0; idx--) /* for every char in buf */
  34.             if (iop->_ptr[idx] == '\n')      /* if it's LF */
  35.               adjust--;                      /* there was a CR also */
  36.     }
  37.     else if (iop->_flag&(_IOWRT|_IORW)) {
  38.         adjust = 0;
  39.         if (iop->_flag&_IOWRT && iop->_base && (iop->_flag&_IONBF)==0)
  40.         {
  41.           adjust = iop->_ptr - iop->_base;
  42.           if (iop->_flag & _IOTEXT)
  43.             for (idx=0; idx<adjust; idx++)
  44.               if (iop->_base[idx] == '\n')
  45.                 adjust++;
  46.         }
  47.     } else
  48.         return(-1);
  49.     tres = lseek(fileno(iop), 0L, 1);
  50.     if (tres<0)
  51.         return(tres);
  52.     tres += adjust;
  53.     return(tres);
  54. }
  55.